<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML >
<html>

<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="content-type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" href="<%=basePath%>css/sales.css">
	<script type="text/javascript" src="<%=basePath%>js/jquery-1.12.0.min.js"></script>
	<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
	<script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
	<script type="text/javascript">
        //根据id删除sales表中的数据
        function deleteByID(sid){
            var f=confirm("你确认要删除吗？");
            if(f){
                var sid=document.getElementById("sid").value=sid;
                var form1=document.getElementById("form1");
                form1.submit();
            }
        }
        function findById(sid){
            var id=document.getElementById("id").value=sid;
            var findForm=document.getElementById("findForm");
            findForm.submit();
        }
        function copyOne(id) {
            location.href="copySales/"+id;
        }
	</script>

	<script type="text/javascript">
        //重置当前页面


        window.onload=function(){
            var olink=document.getElementById("link");
            var odiv=document.getElementById("thediv");

            var status='${sessionScope.dstatus}';
            if(status!='' && status!=null){
                odiv.style.display=status;
            }
            //将显示隐藏的状态保存到后台(odiv)
            //alert("odiv----"+odiv);
            var status=odiv.style.display;
            // alert("odiv的状态----"+status);

            olink.onclick=function(){
                if(odiv.style.display=="none"){
                    odiv.style.display='block';
                }else{
                    odiv.style.display='none';
                }
                var status2=odiv.style.display;
                document.getElementById("dstatus").value=status2;
                return false;
            };
            var b = 0;
            $("#button").click(function(){
                // alert("1111");
                console.log( $("#sname").val().length);
                if($("#status").val().length==0||$("#stype").val().length==0||$("#money").val().length==0||$("#sleader").val().length==0){
                    alert('业务状态，金额，销售负责人，业务类别为必填的，请仔细填写---');

                }else{
                    var insertForm1=document.getElementById("insertForm1");
                    insertForm1.submit();
                }
            });
        }
        function gotoPage(pageNo) {

            if(pageNo==0){
                document.getElementById("pageNo").value = document.getElementById("inputPage").value;
                document.getElementById("setPageSize").value = document.getElementById("getPageSize").value;
            }else {
                document.getElementById("pageNo").value = pageNo;
            }
            var forms = document.getElementById("form2");
            forms.action = "selectByAll";
            forms.submit();
        }
        //实现筛选汇总
        function SumOfLeaders() {
            var leaders_checks = $('.leaders_check');
            var sStates_check = $('.serviceStates_check');
            //清空金额和数量
            for (var i=0;i<sStates_check.length;i++){
                var tab = $('.table1')[0];
                var tr = tab.children[0].children;
                tr[i+1].children[2].innerText="";
                tr[i+1].children[3].innerText="";
            }
            //未勾选的负责人数量
            var sum=0;
            for( var i=0;i<leaders_checks.length;i++){
                if(leaders_checks[i].checked==false){
                    sum+=1;
                }
            }
            if(sum==leaders_checks.length){
                for( var i=0;i<sStates_check.length;i++){
                    sStates_check[i].checked=false;
                }
            }else {
                for( var i=0;i<sStates_check.length;i++){
                    sStates_check[i].checked=true;
                }
            }
            //拿到要遍历的数据(非当前页数据而是所有数据)
            var xx = $('.xx');
            var moneys = 0;
            var count=0;
            // var input = $(".count_money")[0];//先拿到input的值
            // var input2 = $(".count_people")[0];
            for (var i = 0; i < leaders_checks.length; i++) {
                if (leaders_checks[i].checked == true) {
                    for (var k = 0; k < sStates_check.length; k++) {
                        for (var j = 0; j < xx.length; j++) {
                            var leaders_names = xx[j].children[5].innerText;//负责人
                            var sStatus = xx[j].children[1].innerText;//业务状态
                            if (leaders_names == leaders_checks[i].value && sStatus == sStates_check[k].value) {
                                // 首先拿到TABLE
                                var tab = $('.table1')[0];
                                var tr = tab.children[0].children;

                                if (tr[k + 1].children[2].innerText != "") {
                                    tr[k + 1].children[2].innerText = Math.floor((Number(tr[k + 1].children[2].innerText) + Number(xx[j].children[4].innerText))*100)/100;
                                } else {
                                    tr[k + 1].children[2].innerText = xx[j].children[4].innerText;
                                    console.log(xx[j].children[4].innerText + " k:" + k);
                                }
                                if (tr[k + 1].children[3].innerText != "") {
                                    tr[k + 1].children[3].innerText = Number(tr[k + 1].children[3].innerText) + Number(1);
                                } else {
                                    tr[k + 1].children[3].innerText = 1;
                                }
                            }
                        }
                    }
                }
            }
            // var moneys=Math.floor(moneys * 10000) / 10000;
            // input.placeholder=moneys;
            // input2.placeholder=count;
            for (var i=0;i<sStates_check.length;i++){
                if(tr[i+1].children[2].innerText==null || tr[i+1].children[2].innerText==""){
                    tr[i+1].children[2].innerText="0.00";
                }
                if(tr[i+1].children[3].innerText==null || tr[i+1].children[3].innerText==""){
                    tr[i+1].children[3].innerText="0";
                }
                var tab = $('.table1')[0];
                var tr = tab.children[0].children;
                moneys+=Number(tr[i+1].children[2].innerText);
                count+=Number(tr[i+1].children[3].innerText);
            }
            //给总金额和总数量赋值
            var moneys=Math.floor(moneys * 100) / 100;
            document.getElementsByClassName("sumMoney")[0].innerText=moneys;
            document.getElementsByClassName("countPeople")[0].innerText=count;
        }

        //实现业务状态的checkbox筛选
        function SumOfServiceStates() {
            //判断业务状态是不是一个也没勾选
            var sStatesIsNull=true;
            var leadersIsNull=true;
            var leaders_checks = $('.leaders_check');
            var sStates_check = $('.serviceStates_check');

            //清空金额和数量
            for (var i=0;i<sStates_check.length;i++){
                var tab = $('.table1')[0];
                var tr = tab.children[0].children;
                tr[i+1].children[2].innerText="";
                tr[i+1].children[3].innerText="";
            }

            var xx = $('.xx');
            var moneys = 0;
            var count = 0;
            // var input = $(".count_money")[0];//先拿到input的值
            // alert(input.placeholder);
            // var input2 = $(".count_people")[0];

            for (var i = 0; i < sStates_check.length; i++) {
                if(sStates_check[i].checked == true){
                    sStatesIsNull=false;
                }
            }
            for (var i = 0; i < leaders_checks.length; i++) {
                if(leaders_checks[i].checked == true){
                    leadersIsNull=false;
                }
            }
            //勾选
            // sStates_check[i].checked="checked";
            //一个业务状态也没勾选
            if(sStatesIsNull==true){
                for( var i=0;i<leaders_checks.length;i++){
                    leaders_checks[i].checked=false;
                }
            }//一个销售负责人也没勾选
            else if(leadersIsNull==true){
                for (var k = 0; k < sStates_check.length; k++) {
                    if (sStates_check[k].checked == true) {
                        for (var j = 0; j < xx.length; j++) {
                            var sStatus = xx[j].children[1].innerText;//业务状态
                            if (sStatus == sStates_check[k].value) {
                                // 首先拿到TABLE
                                var tab = $('.table1')[0];
                                var tr = tab.children[0].children;

                                if (tr[k + 1].children[2].innerText != "") {
                                    tr[k + 1].children[2].innerText = Math.floor((Number(tr[k + 1].children[2].innerText) + Number(xx[j].children[4].innerText))*100)/100;
                                } else {
                                    tr[k + 1].children[2].innerText = xx[j].children[4].innerText;
                                    console.log(xx[j].children[4].innerText + " k:" + k);
                                }
                                if (tr[k + 1].children[3].innerText != "") {
                                    tr[k + 1].children[3].innerText = Number(tr[k + 1].children[3].innerText) + Number(1);
                                } else {
                                    tr[k + 1].children[3].innerText = 1;
                                }
                            }
                        }
                    }
                }
            }//都有勾选
            else {
                for (var i = 0; i < leaders_checks.length; i++) {
                    if (leaders_checks[i].checked == true) {
                        for (var k = 0; k < sStates_check.length; k++) {
                            if (sStates_check[k].checked == true) {
                                for (var j = 0; j < xx.length; j++) {
                                    var leaders_names = xx[j].children[5].innerText;//负责人
                                    var sStatus = xx[j].children[1].innerText;//业务状态
                                    if (leaders_names == leaders_checks[i].value && sStatus == sStates_check[k].value) {
                                        // 首先拿到TABLE
                                        var tab = $('.table1')[0];
                                        var tr = tab.children[0].children;

                                        if (tr[k + 1].children[2].innerText != "") {
                                            tr[k + 1].children[2].innerText =Math.floor((Number(tr[k + 1].children[2].innerText) + Number(xx[j].children[4].innerText))*100)/100;
                                        } else {
                                            tr[k + 1].children[2].innerText = xx[j].children[4].innerText;
                                            console.log(xx[j].children[4].innerText + " k:" + k);
                                        }
                                        if (tr[k + 1].children[3].innerText != "") {
                                            tr[k + 1].children[3].innerText = Number(tr[k + 1].children[3].innerText) + Number(1);
                                        } else {
                                            tr[k + 1].children[3].innerText = 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // var moneys=Math.floor(moneys * 100) / 100;
            // input.placeholder=moneys;
            // input2.placeholder=count;
            for (var i=0;i<sStates_check.length;i++){
                if(tr[i+1].children[2].innerText==null || tr[i+1].children[2].innerText==""){
                    tr[i+1].children[2].innerText="0.00";
                }
                if(tr[i+1].children[3].innerText==null || tr[i+1].children[3].innerText==""){
                    tr[i+1].children[3].innerText="0";
                }
                var tab = $('.table1')[0];
                var tr = tab.children[0].children;
                moneys+=Number(tr[i+1].children[2].innerText);
                count+=Number(tr[i+1].children[3].innerText);
            }
            //给总金额和总数量赋值
            var moneys=Math.floor(moneys * 100) / 100;
            document.getElementsByClassName("sumMoney")[0].innerText=moneys;
            document.getElementsByClassName("countPeople")[0].innerText=count;
        }


        //进行导出Excel的操作
        function excel(type) {
            var form = document.getElementById("form2");
            form.action = "exportSale/" + type;
            form.submit();

        }
	</script>
	<title>sales</title>
</head>
<body>
<h2>销售汇总表</h2>
<div id="table">
	<div>
		<div class="title-nav">
			<a href="#" id="link" class="btn return">汇总</a>
			<a class="btn return"  href="<%=basePath%>tojump" >首页</a>
			<a class="btn return" href="javaScript:excel(1)">导出当前页</a>
			<a class="btn return" href="javaScript:excel(2)">导出所有</a>
			<input class="btn return" type="button"  value="搜索" onclick="gotoPage(${list.pageNo})"/>
			<a class="btn return" href="/findSales">重置</a>
		</div>
		<!-- 先对这些数据进行简单的多条件查询 -->
		<form id="form2" action="selectByAll" method="post">
			<input type="hidden" name="dstatus" id="dstatus" value="${sessionScope.dstatus}">
			<input type="hidden" name="pageNo" id="pageNo" value="${list.pageNo}"/>
			<input type="hidden" name="setPageSize" id="setPageSize" value="${list.pageSize}"/>
			<div id="thediv" style="display: none;">
				<table  id="t1" class="table1">
					<tr><td>销售负责人</td><td>业务状态</td><td>单项金额(万元)</td><td>数量</td></tr>
					<!-- 判断两个集合的长度,先遍历长的那个集合-->
					<c:if test="${fn:length(sessionScope.leaders)>=fn:length(sessionScope.status)}">
						<c:forEach var="leader" items="${sessionScope.leaders}" varStatus="status">
							<c:choose>
								<c:when test="${status.index+1-fn:length(sessionScope.status)<=0}">
									<c:forEach var="sta" items="${sessionScope.status}" begin="${status.index}" end="${status.index}">
										<tr>
											<td>
												<li class="leaders">
													<input class="leaders_check" type="checkbox" value="${leader.paramValue}" name="sleader" onclick="javaScript:SumOfLeaders()" <c:if test="${fn:indexOf(requestScope.sleader,leader.paramValue)!='-1'}">checked="checked"</c:if>/>${leader.paramValue}
												</li>
											</td>
											<td>

												<li>
													<input class="serviceStates_check" type="checkbox" value="${sta.paramValue}"  name="stype" onclick="javaScript:SumOfServiceStates()" <c:if test="${fn:indexOf(requestScope.stype,sta.paramValue)!='-1'}">checked="checked"</c:if>/>${sta.paramValue}
												</li>

											</td>
											<td></td>
											<td></td>
										</tr>
									</c:forEach>
								</c:when>
								<c:otherwise>
									<tr>
										<td>
											<li class="leaders">
												<input class="leaders_check" type="checkbox" value="${leader.paramValue}"  name="sleader" onclick="javaScript:SumOfLeaders()" <c:if test="${fn:indexOf(requestScope.sleader,leader.paramValue)!='-1'}">checked="checked"</c:if>/>${leader.paramValue}
											</li>
										</td>
										<td>
											<li >

											</li>
										</td>
										<td></td>
										<td></td>
									</tr>
								</c:otherwise>
							</c:choose>
						</c:forEach>
						<tr><td></td><td>总计</td><td class="sumMoney"></td><td class="countPeople"></td></tr>
					</c:if>
					<c:if test="${fn:length(sessionScope.leaders)<fn:length(sessionScope.status)}">
						<c:forEach var="sta" items="${sessionScope.status}" varStatus="status">
							<c:choose>
								<c:when test="${status.index+1-fn:length(sessionScope.leaders)<=0}">
									<c:forEach var="leader" items="${sessionScope.leaders}" begin="${status.index}" end="${status.index}">
										<tr>
											<td>
												<li class="leaders">
													<input class="leaders_check" type="checkbox" value="${leader.paramValue}" name="sleader" onclick="javaScript:SumOfLeaders()" <c:if test="${fn:indexOf(requestScope.sleader,leader.paramValue)!='-1'}">checked="checked"</c:if>/>${leader.paramValue}
												</li>
											</td>
											<td>

												<li>
													<input class="serviceStates_check" type="checkbox" value="${sta.paramValue}"  name="stype" onclick="javaScript:SumOfServiceStates()" <c:if test="${fn:indexOf(requestScope.stype,sta.paramValue)!='-1'}">checked="checked"</c:if>/>${sta.paramValue}
												</li>

											</td>
											<td></td>
											<td></td>
										</tr>
									</c:forEach>
								</c:when>
								<c:otherwise>
									<tr>
										<td>
											<li class="leaders">

											</li>
										</td>
										<td>
											<li >
												<input class="serviceStates_check" type="checkbox" value="${sta.paramValue}"  name="stype" onclick="javaScript:SumOfServiceStates()" <c:if test="${fn:indexOf(requestScope.stype,sta.paramValue)!='-1'}">checked="checked"</c:if>/>${sta.paramValue}
											</li>
										</td>
										<td></td>
										<td></td>
									</tr>
								</c:otherwise>
							</c:choose>
						</c:forEach>
						<tr><td></td><td>总计</td><td class="sumMoney"></td><td class="countPeople"></td></tr>
					</c:if>
				</table>

			</div>
		</form>
	</div>
	<br>
	<table class="sale_tab">
		<thead>
		<tr>
			<th>序号</th>
			<th>业务状态</th>
			<th>客户名称</th>
			<th>业务类别</th>
			<th>金额（万元）</th>
			<th>销售负责人</th>
			<th>简要说明</th>
			<th>工作目标要点</th>
			<th>工作绩效目标</th>
			<th>时间</th>
			<th>操作</th>
		</tr>
		</thead>
		<tbody>
		<c:forEach items="${requestScope.list.data}" var="sa" >
			<tr class="xx1" v-for="(item,index) in newsList">
				<td class="id">${sa.id}</td>
				<td class="status">${sa.status}</td>
				<td class="sname">${sa.sname}</td>
				<td class="stype">${sa.stype}</td>
				<td class="money">${sa.money}</td>
				<td class="sleader">${sa.sleader}</td>
				<td class="content">${sa.content}</td>
				<td class="point">${sa.point}</td>
				<td class="target">${sa.target}</td>
				<td class="status">${sa.update_time}</td>
				<td class="caozuo">
					<input type="button" value="编辑" onclick="javaScript:findById(${sa.id})"/>
					<input type="button" value="删除" onclick="javaScript:deleteByID(${sa.id})"/>
					<input type="button" value="复制" onclick="javaScript:copyOne(${sa.id})"/>
				</td>
			</tr>
		</c:forEach>

		<c:forEach items="${sessionScope.allSales.data}" var="sa" >
			<tr style="display:none" class="xx" >
				<td>${sa.id}</td>
				<td>${sa.status}</td>
				<td>${sa.sname}</td>
				<td>${sa.stype}</td>
				<td>${sa.money}</td>
				<td>${sa.sleader}</td>
				<td>${sa.content}</td>
				<td>${sa.point}</td>
				<td>${sa.target}</td>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<form action="deletByID" id="form1">
		<input type="hidden" id="sid" name="sid"/>

	</form>
	<form action="findByID" id="findForm">
		<input type="hidden" id="id" name="id"/>

	</form>
	<!-- 引入分页 -->
	<jsp:include page="jsp/page.jsp" flush="true"/>

			<form action="insertSales" method="post" id="insertForm1">
					<%--栅格--%>
					<div class="container">
						<div class="row box">
							<div class="col-md-3 box1">
								<span>业务状态<b>*</b></span>
								<select name="status" id="status">
									<c:forEach items="${sessionScope.status}" var="sta">
										<option>${sta.paramValue}</option>
									</c:forEach>
								</select>
							</div>
							<div class="col-md-3 box1">
								<span>客户名称</span>
								<input type="text"  name="sname"  placeholder="客户名称" id="sname" value=""/>
							</div>
							<div class="col-md-3 box1">
								<span>业务类别 <b>*</b></span>
								<select name="stype" id="stype">
									<c:forEach items="${sessionScope.yTypes}" var="ytype">
										<option>${ytype.paramValue}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						<div class="row box">
							<div class="col-md-3 box1">
								<span>金额<b>*</b></span>
								<input type="text"  name="money"  placeholder="金额（万元）" id="money" onkeyup="clearNoNum(this)" />
							</div>
							<div class="col-md-3 box1">
								<span>销售负责人<b>*</b></span>
								<select name="sleader" id="sleader">
									<c:forEach items="${sessionScope.leaders}" var="leader">
										<option>${leader.paramValue}</option>
									</c:forEach>
								</select>
							</div>
							<div class="col-md-3 box1">
								<span>工作绩效</span>
								<input type="text"  name="target" placeholder="工作绩效目标（A）" id="target"/>
							</div>
						</div>
						<div class="row box">
							<div class="col-md-3 box1">
								<span>工作目标要点</span>
								<textarea cols="30" rows="6" name="point" placeholder="工作目标要点" id="point"></textarea>
							</div>
							<div class="col-md-3 box1">
								<span>简要说明</span>
								<textarea cols="30" rows="6" name="content" id="content"></textarea>
							</div>
							<div class="col-md-3 box1">
								<span>注:<b>*</b>为必填项</span>
								<input type="button" value="新增" id="button" />
							</div>
						</div>
					</div>
			</form>

</div>
</body>
</html>